package com.jeecms.wicool.action.product;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.orders.Orders;
import com.jeecms.wicool.entity.product.Product;
import com.jeecms.wicool.entity.product.ProductCate;
import com.jeecms.wicool.entity.product.ProductSku;
import com.jeecms.wicool.entity.product.VIPProduct;
import com.jeecms.wicool.entity.util.Dictionary2;
import com.jeecms.wicool.manager.orders.OrdersMng;
import com.jeecms.wicool.manager.product.ProductCateMng;
import com.jeecms.wicool.manager.product.ProductMng;
import com.jeecms.wicool.manager.product.ProductSkuMng;
import com.jeecms.wicool.manager.product.VIPProductMng;
import com.jeecms.wicool.manager.util.DictionaryMng;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jeecms.common.page.SimplePage.cpn;

/**
 * Created by cuiyanlei on 15/6/16.
 */
@Controller
@RequestMapping("/vipProduct")
public class VIPProductActOld {

    private static final Logger logger = LoggerFactory.getLogger(VIPProductActOld.class);

    @RequestMapping("v_list.do")
    public String list(String productName, String userLevel, Integer pageNo, HttpServletRequest request, ModelMap model) {
        Pagination pagination = manager.getPage(productName, userLevel, cpn(pageNo), CookieUtils.getPageSize(request));
        model.addAttribute("pagination", pagination);
        List<VIPProduct> products = (List<VIPProduct>) pagination.getList();
        model.put("productName", productName);
        model.put("userLevel", userLevel);
        model.put("products", products);
        return "vipProduct/vip_product_list";
    }

    @RequestMapping("v_edit.do")
    public String toEdit(Integer id, HttpServletRequest request, ModelMap model) {
        VIPProduct vipProduct = manager.findById(id);
        Product rawProduct = productMng.findById(vipProduct.getProductId());
        ProductSku spec = productSkuMng.findById(vipProduct.getProductSpecId());

        List<ProductCate> tempCates = productCateMng.getAll();
        tempCates.remove(rawProduct.getProductCate());

        List<Product> tempProducts = productMng.getAll(rawProduct.getProductCate().getId());
        tempProducts.remove(rawProduct);

        List<ProductSku> tempSpecs = productSkuMng.getListProductSku(rawProduct.getId());
        tempSpecs.remove(spec);

        ImmutableSet<ProductCate> productCates = ImmutableSet.<ProductCate>builder().add(rawProduct.getProductCate()).addAll(tempCates).build();
        ImmutableSet<Product> products = ImmutableSet.<Product>builder().add(rawProduct).addAll(tempProducts).build();
        ImmutableSet<ProductSku> productSpecifications = ImmutableSet.<ProductSku>builder().add(spec).addAll(tempSpecs).build();

        model.addAttribute("productCates", productCates);
        model.addAttribute("products", products);
        model.addAttribute("specifications", productSpecifications);
        model.addAttribute("userLevel", vipProduct.getUserLevel());
        model.addAttribute("limitCount", vipProduct.getLimitCount());
        model.addAttribute("vipProductId", id);
        return "vipProduct/to_edit_vip_product";
    }

    @RequestMapping("o_update.do")
    public String update(Integer id, Integer productId, Integer productSpecId, String userLevel, Integer limitCount, HttpServletRequest request, ModelMap model) {
        Product product = productMng.findById(productId);
        ProductSku spec = productSkuMng.findById(productSpecId);

        VIPProduct vipProduct = manager.findById(id);
        vipProduct.setProductId(productId);
        vipProduct.setProductName(product.getProductName());
        vipProduct.setProductSpecId(productSpecId);
        vipProduct.setProductSpecName(spec.getSkuName());
        vipProduct.setUserLevel(userLevel);
        vipProduct.setLimitCount(limitCount);
        vipProduct.setUpdateTime(new Timestamp(DateTime.now().getMillis()));

        model.addAttribute("message", "global.success");
        manager.updateByUpdater(new Updater<VIPProduct>(vipProduct));
        return toEdit(id, request, model);
    }

    @RequestMapping("o_save.do")
    public String save(Integer productId, Integer productSpecId, String userLevel, Integer limitCount) {
        Product product = productMng.findById(productId);
        ProductSku spec = productSkuMng.findById(productSpecId);
        VIPProduct bean = new VIPProduct(null, productId, product.getProductName(), productSpecId, spec.getSkuName(), userLevel, limitCount, -1, new Timestamp(DateTime.now().getMillis()), new Timestamp(DateTime.now().getMillis()));
        manager.save(bean);
        return "redirect:v_list.do";
    }

    @RequestMapping("o_changeStatus.do")
    public String changeStatus(Integer id, Integer status) {
        VIPProduct bean = manager.findById(id);
        bean.setStatus(status);
        bean.setUpdateTime(new Timestamp(DateTime.now().getMillis()));
        manager.updateByUpdater(new Updater<VIPProduct>(bean));
        return "redirect:v_list.do";
    }

    @RequestMapping("v_add.do")
    public String add(HttpServletRequest request, ModelMap model) {
        List<ProductCate> productCates = productCateMng.getAll();
        List<Product> products = Lists.newArrayList();
        List<ProductSku> specifications = Lists.newArrayList();
        model.addAttribute("productCates", productCates);
        model.addAttribute("products", products);
        model.addAttribute("specifications", specifications);
        return "vipProduct/add_vip_product";
    }

    @RequestMapping("o_delete.do")
    public String delete(Integer[] ids) {
        for (Integer id : ids) {
            manager.deleteById(id);
        }
        return "redirect:v_list.do";
    }

    @SuppressWarnings("unchecked")
	@RequestMapping("o_search_orders.do")
	public String searchOrders(Integer id,Integer pageNo,HttpServletRequest request,ModelMap model){
		Pagination pagination = ordersMng.getPage(3,id,cpn(pageNo),CookieUtils.getPageSize(request));
		List<Orders> listOrders =(List<Orders>) pagination.getList();
		List<Dictionary2> listDic=DicMng.getList("order_status");
		List<Dictionary2> listDic2=DicMng.getList("pay_type");
		List<Dictionary2> listDic3=DicMng.getList("activity_type");
		Map<String,String> map = new HashMap<String,String>();
		Map<String,String> map2 = new HashMap<String,String>();
		Map<String,String> map3 = new HashMap<String,String>();
		 for(Dictionary2 d : listDic){
			 map.put(d.getDicCode().toString(), d.getDicName());
		 }
		 for(Dictionary2 d : listDic2){
			 map2.put(d.getDicCode().toString(), d.getDicName());
		 }
		 for (Dictionary2 d : listDic3) {
			 map3.put(d.getDicCode().toString(), d.getDicName());
		}
		 for(Orders o : listOrders){
			 if(map.containsKey(o.getStatus().toString())){
				 o.setStatusName(map.get(o.getStatus().toString()));
			 }
			 if(map2.containsKey(o.getPayway().toString())){
				 o.setPaywayName(map2.get(o.getPayway().toString()));
			 }
			 if(map3.containsKey(o.getType().toString())){
				 o.setTypeName(map3.get(o.getType().toString()));
			 }
		 }
		model.addAttribute("pagination", pagination);
		model.addAttribute("activityName", "VIP商品管理");
		model.addAttribute("id", id);
		return "orders/list_activity_orders";
	}
    
    @Autowired
    private VIPProductMng manager;
    @Autowired
    private ProductCateMng productCateMng;
    @Autowired
    private ProductMng productMng;

    @Autowired
    private ProductSkuMng productSkuMng;
    
    @Autowired
	private DictionaryMng DicMng;
	
	@Autowired
	private OrdersMng ordersMng;
}
